connect_error) { die(json_encode(["message" => "Connection failed: " . $conn->connect_error])); } if ($_SERVER["REQUEST_METHOD"] === "POST") { $name = trim($_POST['name']); $surname = trim($_POST['surname']); $phone = trim($_POST['phone']); $email = trim($_POST['email']); $appointment_date = trim($_POST['appointment-date']); $appointment_time = trim($_POST['appointment-time']); $appointment_type = trim($_POST['appointment-type']); // Check empty fields (email optional) if (empty($name) || empty($surname) || empty($phone) || empty($appointment_date) || empty($appointment_time) || empty($appointment_type)) { die(json_encode(["message" => "Please fill in all required fields."])); } // Validate name and surname if (!preg_match("/^[a-zA-Z\s]+$/", $name) || !preg_match("/^[a-zA-Z\s]+$/", $surname)) { die(json_encode(["message" => "Invalid name or surname format."])); } // Validate phone if (!preg_match("/^\d{10}$/", $phone)) { die(json_encode(["message" => "Phone number must be 10 digits."])); } // Validate email only if provided if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) { die(json_encode(["message" => "Invalid email format."])); } // ✅ CHECK FOR DUPLICATE BOOKING (same date and time) $check = $conn->prepare("SELECT id FROM `medi-works booking` WHERE appointment_date = ? AND appointment_time = ?"); $check->bind_param("ss", $appointment_date, $appointment_time); $check->execute(); $check->store_result(); if ($check->num_rows > 0) { die(json_encode(["message" => "This time slot is already booked. Please choose another time."])); } $check->close(); // Insert booking $stmt = $conn->prepare("INSERT INTO `medi-works booking` (name, surname, phone, email, appointment_date, appointment_time, appointment_type) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("sssssss", $name, $surname, $phone, $email, $appointment_date, $appointment_time, $appointment_type); if ($stmt->execute()) { echo json_encode(["message" => "Booking created successfully"]); } else { echo json_encode(["message" => "Error creating booking: " . $stmt->error]); } $stmt->close(); $conn->close(); exit; } ?> Bookings - Medi Works